const jwt = require('jsonwebtoken');
const fs = require('fs');
const path = require('path');


/**
 * token生成器
 */
class Jwt {
    constructor() {
        this.verify = { algorithm: 'RS256' };
    }

    /**
     * 生成token
     * @param {*} data 
     */
    generateToken(data) {
        let cert = fs.readFileSync(path.join(__dirname, '../config/rsa_private_key.pem'));
        return jwt.sign({ data }, cert, this.verify);
    }

    /**
     * 验证token
     * @param {*} token 
     */
    verifyToken(token) {
        let cert = fs.readFileSync(path.join(__dirname, '../config/rsa_public_key.pem'));
        return jwt.verify(token, cert, this.verify);
    }
}

module.exports = global.buildClass(Jwt);